package com.fl.nj.controller;

import com.fl.nj.entity.SyncStrategy;
import com.fl.nj.service.SyncStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 同步策略Controller
 * 提供RESTful API接口，供Vue前端调用管理同步策略
 */
@RestController
@RequestMapping("/api/sync-strategy")
public class SyncStrategyController {
    @Autowired
    private SyncStrategyService syncStrategyService;

    /**
     * 获取所有同步策略列表
     * @return 同步策略列表
     */
    @GetMapping
    public ResponseEntity<List<SyncStrategy>> getAll() {
        List<SyncStrategy> strategyList = syncStrategyService.getAll();
        return ResponseEntity.ok(strategyList);
    }

    /**
     * 根据ID获取同步策略
     * @param id 策略ID
     * @return 同步策略对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<SyncStrategy> getById(@PathVariable Long id) {
        SyncStrategy strategy = syncStrategyService.getById(id);
        if (strategy == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(strategy);
    }

    /**
     * 根据策略名称获取同步策略
     * @param strategyName 策略名称
     * @return 同步策略对象
     */
    @GetMapping("/name/{strategyName}")
    public ResponseEntity<SyncStrategy> getByStrategyName(@PathVariable String strategyName) {
        SyncStrategy strategy = syncStrategyService.getByStrategyName(strategyName);
        if (strategy == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(strategy);
    }

    /**
     * 添加同步策略
     * @param strategy 同步策略对象
     * @return 添加结果
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> save(@RequestBody SyncStrategy strategy) {
        Map<String, Object> result = new HashMap<>();
        try {
            syncStrategyService.save(strategy);
            result.put("success", true);
            result.put("message", "添加成功");
            result.put("data", strategy);
            return ResponseEntity.status(HttpStatus.CREATED).body(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 更新同步策略
     * @param id 策略ID
     * @param strategy 同步策略对象
     * @return 更新结果
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> update(@PathVariable Long id, @RequestBody SyncStrategy strategy) {
        strategy.setId(id);
        Map<String, Object> result = new HashMap<>();
        try {
            syncStrategyService.update(strategy);
            result.put("success", true);
            result.put("message", "更新成功");
            result.put("data", strategy);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 删除同步策略
     * @param id 策略ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> delete(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            syncStrategyService.delete(id);
            result.put("success", true);
            result.put("message", "删除成功");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
}